package com.qinghuan.threads.test;

/**
 * @Author: dubao
 * @Date: 2022/5/17 21:19
 * @Description:
 *
 */

/**
 * wait:  1、只会释放当前调用wait的共享变量的锁，该线程在此wait之后的代码不执行了;
 *        2、共享变量wait后，如果再interrupt，会报错
 * join:  等待线程执行结束，之后才会执行下面的代码
 * notify: 唤醒某个在就绪队列中的线程
 * interrupt: 终止的方法
 * sleep: 睡眠的方法
 * yield:  将该线程的CPU时间片暂时性的放开，然后在就绪的队列中选择优先级最高的，或者说是随机的选取一个
 */
public class ChapterOne {

    private static volatile Object resourceA = new Object();
    private static volatile Object resourceB = new Object();

    public static void main(String[] args) throws InterruptedException {
        Thread threadA = new Thread(() -> {
            synchronized (resourceA) {
                System.out.println("threadA use resourceA");
                try {
                    synchronized (resourceB) {
                        System.out.println("threadA use resourceB");

                        resourceA.wait();

                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("can you see?");
            }
        });

        Thread threadB = new Thread(() -> {
            synchronized (resourceA) {
                System.out.println("threadB use resourceA");
            }
            synchronized (resourceB) {
                System.out.println("threadB use resourceB");
            }
        });

        threadA.start();
//        threadB.start();
//
//        Thread.sleep(10000);
        threadA.interrupt();

        threadA.join();
        threadB.join();


    }

}
